package pro.syj.db;

import java.sql.*;

public class DB {
	public static String driver = "com.mysql.cj.jdbc.Driver";
    
    //访问本机的mysql数据库,格式 jdbc:数据库://本地端口3306/数据库名?跨时区参数
	public static String url = "jdbc:mysql://localhost:3306/projectdb?characterEncoding=UTF-8";
	public static String username = "root";
	public static String password = "111111";
	
	/*
	 * 封装1：注册连接，返回一个连接conn
	*/
    public static Connection getConnection(){
		Connection conn = null;
		 try {
			 	//初始化驱动类
	            Class.forName(driver);
	            conn = DriverManager.getConnection(url,username, password);
	            //如果忘记导包，就会抛出ClassNotFoundException
	        } catch (ClassNotFoundException e) { 				
	            e.printStackTrace();
	        }catch (SQLException e) {							
	            e.printStackTrace();
	        }
		 return conn;
	}
    
    
    /*
	 * 封装2：释放资源（结果集，语句，连接） 
	 */
	public static void release(ResultSet rs ,Statement stmt , Connection conn){
		if(rs!=null){
			try {
				rs.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
			rs=null;
		}
		if(stmt!=null){
			try {
				stmt.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
			stmt=null;
		}
		if(conn!=null){
			try {
				conn.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
			conn=null;
		}
	}
}
